$authr()

require('../css/mineInfo.less')

require('../font/iconfont.css')

require('../lib/weui/weui.css')

const weui = require('../lib/weui/weui.min.js')
const axios = require('../lib/axios/axios.js')



// 主页展示

document.ready(function () {





    let backMe = document.querySelector('#backMe');
    backMe.addEventListener('click', function () {
        location.href = './mine.html'
    })



    let mePic = document.querySelector('#me-pic');
    let meName = document.querySelector('#nickname');
    let sex = document.querySelector('#sex');
    let birth = document.querySelector('#birth');
    let city = document.querySelector('#city');

    let province = document.querySelector('#province');
    let brief = document.querySelector('#brief');


    let ltCount = document.querySelector("#ltCount");


    // 城市
    let citys = []

    // 省份
    let sf = []


    // 获取数据
    let data = JSON.parse(localStorage.getExpire('data'));
    const uid = data.user.userId

    let isLoad = false

    getUserInfo()

    function getUserInfo() {
        axios.get('http://139.9.177.51:8099/users/accountinfo?userId=' +
            uid).then(res => {

            if (res.data.status != 0) {
                $createMask("icon-dingwei1", "获取信息失败!", 1000)
            } else {

                isLoad = true
                const nickname = res.data.data.nickname
                const sign = res.data.data.sign
                const address = res.data.data.address
                const birthday = res.data.data.birthday
                const gender = res.data.data.gender
                const imgurl = res.data.data.imgurl


                if (gender) sex.innerHTML = gender
                if (nickname) meName.value = nickname
                if (sign) {
                    brief.innerHTML = sign;
                    ltCount.innerHTML = sign.length;
                }

                if (address) {
                    let a = address.split(",")
                    city.innerHTML = a[0];
                    province.innerHTML = a[1];
                }

                if (imgurl) mePic.src = imgurl;
                let date = new Date(Date.parse(birthday));
                if (birthday) birth.innerHTML = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`



            }
        }).catch(r => {
            console.log(r);
            $createMask("icon-dingwei1", "网络不稳定!", 1000)
        })


    }




    // 保存信息
    const saveInfo = document.querySelector('#saveInfo');
    let eidtObj = {
        imgurl: "",
        nickname: "",
        gender: "",
        birthday: "",
        address: "",
        sign: "",
        userId: uid,
    }


    saveInfo.addEventListener('click', function () {


        if (!isLoad) return

        eidtObj.imgurl = mePic.src.trim()
        eidtObj.nickname = meName.value.trim()
        eidtObj.gender = sex.innerHTML.trim()
        eidtObj.birthday = Date.parse(birth.innerHTML)
        eidtObj.address = [province.innerHTML, city.innerHTML]
        eidtObj.sign = brief.value.trim()


        axios.post('http://139.9.177.51:8099/users/userEdit', eidtObj).then(res => {
            if (res.data.status == 0) {
                $createMask("icon-dingwei1", "修改信息成功!", 1000)
                getUserInfo()
            } else {
                $createMask("icon-dingwei1", "修改信息失败!", 1000)
            }
        }).catch(rea => {
            console.log(rea);
            $createMask("icon-dingwei1", "网络不稳定!", 1000)
        })
    })




    // 修改图片
    let upload = document.querySelector('#upload');
    upload.addEventListener('change', function () {

        let fordata = new FormData();
        fordata.append('imgurl', upload.files[0])

        axios.post(BASE_URL + "/users/upload", fordata).then(res => {

            if (res.data.status == 0) {
                mePic.src = BASE_URL + res.data.data
                return BASE_URL + res.data.data
            }

        }).then(res => { //获取上一个then的返回值
            // 修改图片信息
            axios.post(BASE_URL + "/users/userEdit", {
                userId: uid,
                imgurl: res
            }).then(r => {
                console.log(r);
            })
        })
    })




    // 显示输入的文字的个数
    ltCount.innerHTML = brief.value.length
    brief.addEventListener("input", function () {

        ltCount.innerHTML = this.value.length

    })


    // 点击修改性别
    document.querySelector('#showPicker').addEventListener('click', function () {
        weui.picker([{
            label: '男',
            value: 0
        }, {
            label: '女',
            value: 1
        }], {
            onConfirm: function (result) {
                sex.innerHTML = result[0].label
            },
            title: '请选择性别'
        });
    })



    // 修改生日
    document.querySelector('#showDatePicker').addEventListener('click', function () {
        weui.datePicker({
            start: 1950,
            end: new Date().getFullYear(),
            onConfirm: function (result) {
                birth.innerHTML = `${result[0].value}-${result[1].value}-${result[2].value}`
            },
            title: '请选择生日'
        });
    });




    // 修改省份
    document.querySelector('#showSf').addEventListener('click', function () {
        // 获取省份
        axios.get(`${BASE_URL}/address/province`).then(res => {
            // 获取省份数据
            sf = res.data.data.map(ele => {
                return {
                    label: ele.name,
                    value: ele.addressId
                }
            });
            // 渲染
            weui.picker(sf, {
                onConfirm: function (result) {
                    // 获取的数据刷新到 元素上

                    province.innerHTML = result[0].label
                    showSF(result)
                },
                title: '请选择省市'
            });

        })
    });


    // 修改城市
    function showSF(result) {
        axios.get(`${BASE_URL}/address/city/${result[0].value}`).then(c => {
            citys = c.data.data.map(ele => {
                return {
                    label: ele.name,
                    value: ele.addressId
                }
            })

            document.querySelector('#showCity').addEventListener('click', function () {
                weui.picker(citys, {
                    onConfirm: function (result) {
                        // 获取的数据刷新到 元素上
                        city.innerHTML = result[0].label
                    },
                    title: '请选择城市'
                });
            })
        })
    }


    // if (province) {
    //     showSF(province.innerHTML.trim())
    // }


})